package tool;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import constant.ConfigConstants;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 天气
 * Created by Intellij IDEA.
 * User:  老赵
 * Date:  2023/7/4
 */
public class Weather {

    private static Map<String, String> cityIdMap = new HashMap<>();

    static {
        String filePath = Weather.class.getClassLoader().getResource("json/cityId.txt").getFile();
        File file = new File(filePath);
        JSONArray json = (JSONArray) JSONUtil.readJSON(file, StandardCharsets.UTF_8);
        for (int i = 0; i < json.size(); i++) {
            cityIdMap.put(json.getJSONObject(i).getStr("name"), json.getJSONObject(i).getStr("id"));
        }
    }

    public static void openWeatherStage() {
        Stage weatherStage = new Stage();
        weatherStage.setTitle("天气查询");
        weatherStage.setFullScreen(ConfigConstants.FullScreen);

        // 创建天气查询界面的元素
        Label cityLabel = new Label("城市:");
        TextField cityField = new TextField();
        Button searchButton = new Button("查询");
        // 创建天气文本框
        TextArea textArea1 = new TextArea();
        textArea1.setPrefHeight(200);
        textArea1.setEditable(false);
        TextArea textArea2 = new TextArea();
        textArea2.setPrefHeight(200);
        textArea2.setEditable(false);
        TextArea textArea3 = new TextArea();
        textArea3.setPrefHeight(200);
        textArea3.setEditable(false);
        // 设置查询按钮的点击事件
        searchButton.setOnAction(event -> {
            String city = cityField.getText();
            // 调用查询天气的方法
            List<JSONObject> weather = null;
            try {
                weather = getWeather(city);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            textArea1.setText(ObjectUtil.isNotNull(weather) ? weather.get(0).toStringPretty() : "参数错误");
            textArea2.setText(ObjectUtil.isNotNull(weather) ? weather.get(1).toStringPretty() : "参数错误");
            textArea3.setText(ObjectUtil.isNotNull(weather) ? weather.get(2).toStringPretty() : "参数错误");
        });
        // 创建天气查询界面的布局
        VBox root = new VBox(10);
        root.setPadding(new Insets(10));
        root.getChildren().addAll(cityLabel, cityField, searchButton, textArea1, textArea2, textArea3);
        // 创建天气查询界面的场景
        Scene scene = new Scene(root, 500, 750);
        weatherStage.setScene(scene);
        weatherStage.show();
    }

    public static List<JSONObject> getWeather(String city) throws IOException {
        boolean contains = cityIdMap.containsKey(city);
        if (!contains) {
            return null;
        }
        String cityId = cityIdMap.get(city);
        String url = "http://t.weather.itboy.net/api/weather/city/" + cityId;
        String html = HttpUtil.get(url);
        JSONObject obj = new JSONObject(html);
        JSONArray forecastArray = obj.getJSONObject("data").getJSONArray("forecast");
        List<JSONObject> list = new ArrayList<>(3);
        list.add(forecastArray.getJSONObject(0));
        list.add(forecastArray.getJSONObject(1));
        list.add(forecastArray.getJSONObject(2));
        return list;
    }
}
